package cn.wolfcode.service.impl;

import cn.wolfcode.Utlis.JwtUtlis;
import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.domain.pageBean;
import cn.wolfcode.mapper.EmpMapper;
import cn.wolfcode.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    @Override
    public pageBean page(Integer page, Integer pageSize, String name, Integer gender, Date begin, Date end) {
        PageHelper.startPage(page,pageSize);
        List<Emp> rows = empMapper.page(name,gender,begin,end);
          Page<Emp> p = (Page<Emp>)rows;
        return new pageBean(p.getTotal(),p.getResult());
    }

    @Override
    public void delete(List<Long> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void add(Emp emp) {
        empMapper.add(emp);
    }

    @Override
    public Emp selById(Long id) {
        return empMapper.selById(id);
    }

    @Override
    public void update(Emp emp) {
        empMapper.update(emp);
    }

    @Override
    public Result login(Emp emp) {
   Emp emp1 = empMapper.login(emp);
   if(emp1!=null){
       HashMap<String,Object> claims = new HashMap<>();
       claims.put("id",emp1.getId());
       claims.put("username",emp1.getUsername());
       return Result.success(JwtUtlis.createJwt(claims));
   }
        return Result.error("用户名或密码错误");
    }


}
